import subprocess
import gc
import time
import pygetwindow
from pywinauto import Application


# def get_device_status():
#     command = r'wmic path win32_pnpentity where pnpclass="USB" get pnpclass, service, name, deviceid'
#     process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
#     result, error = process.communicate()
#     if error:
#         print('Error:', error)
#     else:
#         print(result)
def get_devmgmt_status():
    # command = r'devmgmt.msc'
    # command = r'control hdwwiz.cpl'
    command = 'explorer {}'.format("d:")
    subprocess.Popen(command)


def get_explorer():
    # Open
    # subprocess.Popen(r'explorer "D:\项目"')
    time.sleep(1)
    # locate window
    explorer_titles = pygetwindow.getAllTitles()
    print(explorer_titles)
    explorer_windows = pygetwindow.getAllWindows()
    # print(explorer_windows)
    for window in explorer_windows:
        print(window.title)
        # if window.title == "项目 - 文件资源管理器":
        #     window.close()
        if window.title == "Program Manager" or window.title == "":
            print('no')
        else:
            print('yes')
            window.close()
    time.sleep(1)
    # Close the window
    # window = explorer_windows[0]
    # app = Application().connect(handle=window._hWnd)
    # app.top_window().close()
    # command = 'gc.collect()'
    # subprocess.Popen(command)


if __name__ == '__main__':
    get_explorer()
    # get_devmgmt_status()
    # get_device_status()